package com.monlyu.action.manuscript;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.monlyu.action.BaseAction;
import com.monlyu.bo.ManuscriptBo;
import com.monlyu.bo.UserBo;
import com.monlyu.vo.ManuScAudit;
import com.monlyu.vo.Manuscript;
import com.monlyu.vo.Manuscript2Image;
import com.monlyu.vo.User;

/*******************************************************************************
 * <b>功能描述☞</b><br>
 * <b>项目名☞</b>NewsSys<br>
 * <b>文件路径☞</b>com.monlyu.action.manuscript.EditManuscriptAction.java<br>
 * <b>开发日期☞</b>2010-2-23 <br>
 * 
 * @author skw
 * @since JDK 5
 * @version 1.0.1
 ******************************************************************************/
public class EditManuscriptAction extends BaseAction {

	private Manuscript manuscript;
	private ManuScAudit manuScAudit;
	private ManuscriptBo manuscriptBo;
	private String lhjz;
	private List<User> allRepUser = Collections.emptyList();
	private String lhbdjzName;
	private String lhbdjzId;
	private List<ManuScAudit> auditRecd = Collections.emptyList();
	private Boolean readOnly;
	private List<String> manuList;
	private String timeUnit;
	private int level1, level2;
	private String result;
	private String images;
	private List<Manuscript2Image> allimages;
	private HashMap users;

	public HashMap getUsers() {
		return users;
	}

	public void setUsers(HashMap users) {
		this.users = users;
	}

	public List<Manuscript2Image> getAllimages() {
		return allimages;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public int getLevel1() {
		return level1;
	}

	public void setLevel1(int level1) {
		this.level1 = level1;
	}

	public int getLevel2() {
		return level2;
	}

	public void setLevel2(int level2) {
		this.level2 = level2;
	}

	public String delRefrence() {
		manuscriptBo.deleteReferce(images, manuscript);
		return ajax("OK");
	}

	public String execute() throws Exception {
		// System.out.println(manuscript.getTimeLong());
		if (manuscript.getId() != null) {
			manuscriptBo.UpdateManuscript(manuscript, lhjz);
			manuscriptBo.update(manuscript, images);
		} else {
			manuscriptBo.CreateManuscript(manuscript, lhjz);
			manuscriptBo.insertImage(manuscript, images);
		}

		return ajax(manuscript.getId());
	}

	public String toEdit() {
		allRepUser = manuscriptBo.getAllRepUser();
		if (manuscript == null) {
			manuscript = new Manuscript();
		}
		if (manuscript != null && manuscript.getId() != null) {
			lhbdjzName = manuscriptBo.getDbLhbdjzName(manuscript);
			lhbdjzId = manuscriptBo.getDbLhbdjzId(manuscript);
			manuscriptBo.load(manuscript);
		}

		allimages = manuscriptBo.findAllImage(manuscript);
		users = manuscriptBo.getAuditUser(manuscript.getAuditZt());
		return view("manuscriptForm.jsp");
	}

	public String toAudit() {
		allRepUser = manuscriptBo.getAllRepUser();
		if (manuscript != null && manuscript.getId() != null) {
			manuscriptBo.load(manuscript);
			auditRecd = manuscriptBo.getAuditRecd(manuscript);
			lhbdjzName = manuscriptBo.getDbLhbdjzName(manuscript);
			lhbdjzId = manuscriptBo.getDbLhbdjzId(manuscript);
		}
		allimages = manuscriptBo.findAllImage(manuscript);
		users = manuscriptBo.getAuditUser(manuscript.getAuditZt());
		return view("manusAuditForm.jsp");
	}

	public String audit() throws Exception {
		try {
			manuscriptBo.AuditManuscript(manuscript, manuScAudit, lhjz);
		} catch (Exception e) {
			return ajax(e.toString());
		}
		return ajax();
	}

	public String delete() {
		manuscriptBo.delete(manuscript);
		return ajax(manuscript.getId());
	}

	public String subAudit() {
		manuscriptBo.subAudit(manuscript);
		return ajax(manuscript.getId());
	}

	public String killMas() {
		manuscriptBo.killMas(manuscript, manuScAudit);
		return ajax("Ok");
	}

	public String returnToApp() {
		manuscriptBo.returnToApp(manuscript, manuScAudit);
		return redirect("manuscript/ListManuscript");
	}

	public String printManus() {
		setDecType(-1);
		manuscriptBo.load(manuscript);
		lhbdjzName = manuscriptBo.getDbLhbdjzName(manuscript);
		result = manuscript.getContent().replaceAll("\\n", "<br/>");
		return view("manuscriptPrint.jsp");
	}

	public String viewRecod() {
		manuscriptBo.load(manuscript);
		auditRecd = manuscriptBo.getAuditRecd(manuscript);
		return view("manuscriptRecord.jsp");
	}

	public String setTimeLongUnit() {
		timeUnit = manuscriptBo.getTimeLongUnit();
		return view("manusTimeSet.jsp");
	}

	public String saveTimeLongUnit() {
		manuscriptBo.saveTimeLongUnit(timeUnit);
		return ajax("操作成功！");
	}

	/**
	 * 稿件内容比较
	 * 
	 * @return
	 * @since chendan 2010-3-27
	 */
	public String compareContent() {
		manuscriptBo.load(manuscript);
		result = manuscriptBo.compareContent(manuscript, level1, level2);
		return view("compareContent.jsp");
	}

	public Manuscript getManuscript() {
		return manuscript;
	}

	public void setManuscript(Manuscript manuscript) {
		this.manuscript = manuscript;
	}

	public ManuscriptBo getManuscriptBo() {
		return manuscriptBo;
	}

	public void setManuscriptBo(ManuscriptBo manuscriptBo) {
		this.manuscriptBo = manuscriptBo;
	}

	public ManuScAudit getManuScAudit() {
		return manuScAudit;
	}

	public void setManuScAudit(ManuScAudit manuScAudit) {
		this.manuScAudit = manuScAudit;
	}

	public String getLhjz() {
		return lhjz;
	}

	public void setLhjz(String lhjz) {
		this.lhjz = lhjz;
	}

	public List<User> getAllRepUser() {
		return allRepUser;
	}

	public void setAllRepUser(List<User> allRepUser) {
		this.allRepUser = allRepUser;
	}

	public String getLhbdjzName() {
		return lhbdjzName;
	}

	public void setLhbdjzName(String lhbdjzName) {
		this.lhbdjzName = lhbdjzName;
	}

	public String getLhbdjzId() {
		return lhbdjzId;
	}

	public void setLhbdjzId(String lhbdjzId) {
		this.lhbdjzId = lhbdjzId;
	}

	public List<ManuScAudit> getAuditRecd() {
		return auditRecd;
	}

	public void setAuditRecd(List<ManuScAudit> auditRecd) {
		this.auditRecd = auditRecd;
	}

	public Boolean getReadOnly() {
		return readOnly;
	}

	public void setReadOnly(Boolean readOnly) {
		this.readOnly = readOnly;
	}

	public List<String> getManuList() {
		return manuList;
	}

	public void setManuList(List<String> manuList) {
		this.manuList = manuList;
	}

	public String getTimeUnit() {
		return timeUnit;
	}

	public void setTimeUnit(String timeUnit) {
		this.timeUnit = timeUnit;
	}

	public String getImages() {
		String a = "";
		for (Manuscript2Image m2i : allimages) {
			if (!a.equals("")) {
				a = a + ",";
			}
			a = a + m2i.getImageId();
		}
		return a;
	}

	public void setImages(String images) {
		this.images = images;
	}

}
